package com.tanhua.dubbo.api.mongo;

import com.tanhua.commons.exception.TanHuaException;
import com.tanhua.model.mongo.*;
import com.tanhua.model.vo.ErrorResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 问卷表接口实现类
 *
 * @author youshengfeng
 * @date 2022/2/20 上午 11:06
 */
@DubboService
public class QuestionnaireApiImpl implements QuestionnaireApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 统计总分 获得并保存人格
     *
     * @param objects
     * @param gender
     * @param userId
     */
    @Override
    public String handIn(List<ObjectId> objects, String gender, Long userId, Integer level) {
        //根据试题id 选择id查询
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(objects));
        //查询获得选项集合
        List<Option> optionList = mongoTemplate.find(query, Option.class);
        //遍历集合获得总分
        List<Integer> scoreList = optionList.stream().map(Option::getScore).collect(Collectors.toList());
        //初始化总分
        Integer zongFen = 0;
        //把每道题的分数加入总分 获得总分

        for (Integer score : scoreList) {
            if (score == null) {
                throw new TanHuaException(ErrorResult.error("999929", "分数异常"));
            }
            zongFen += score;
        }
        Soul soul = new Soul();
        Aggregate aggregate = new Aggregate();
        // Personality personality = new Personality();
        Dimensions dimensions = new Dimensions();


        Integer personalityType = null;
        //判断人格类型
        if (zongFen <=20&&zongFen>=10) {
            //猫头鹰
            personalityType = 1;
            soul.setPersonalityType(1);
            //  personality.setPersonalityType(1);

        } else if (zongFen > 21 & zongFen < 31) {
            //白兔
            personalityType = 2;
            soul.setPersonalityType(2);
            //  personality.setPersonalityType(2);

        } else if (zongFen > 31 & zongFen < 41) {
            //狐狸
            personalityType = 3;
            soul.setPersonalityType(3);
            // personality.setPersonalityType(3);


        } else  {
            //狮子
            personalityType = 4;
            soul.setPersonalityType(4);
            // personality.setPersonalityType(4);

        }
        aggregate.setAggregate(zongFen);
        //  mongoTemplate.insert(personality);
        soul.setUserId(userId);
        soul.setGender(gender);
        soul.setLevel(level);
        soul.setCreated(System.currentTimeMillis());

       // FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        Query query2 = new Query();
        query2.addCriteria(Criteria.where("level").is(level).and("userId").is(userId));

        boolean exists = mongoTemplate.exists(query2, Soul.class);
        Soul soul1 = null;
        if (exists){
            Update update = new Update();
            update.set("personalityType",personalityType);
            mongoTemplate.updateFirst(query2,update,Soul.class);
            query2.addCriteria(Criteria.where("personalityType").is(personalityType));
           soul1= mongoTemplate.findOne(query2,Soul.class);


        }else {
            mongoTemplate.insert(soul);
            Query query1 = new Query();
            query1.addCriteria(Criteria.where("personalityType").is(personalityType));
            soul1=mongoTemplate.findOne(query1, Soul.class);
        }
        return soul1.getId().toHexString();

    }


}
